package com.google.gwt.sample.dynatablerf.client.views;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.sample.dynatablerf.client.presenters.SingleSelectionDataPresenter.SingleSelectionDataDisplay;
import com.google.gwt.sample.dynatablerf.client.theme.GrifoTheme;
import com.google.gwt.sample.dynatablerf.client.util.GrifoConstants;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSearchBox;
import com.google.gwt.sample.dynatablerf.client.widgets.GrifoSuggestOracle;
import com.google.gwt.sample.dynatablerf.client.widgets.HasGrifoSuggestHandler;
import com.google.gwt.sample.dynatablerf.client.widgets.HasMenuItens;
import com.google.gwt.sample.dynatablerf.shared.GrifoSharedConstants;
import com.google.gwt.sample.dynatablerf.shared.ProjetoProxy;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent.HasSelectionChangedHandlers;
import com.google.gwt.view.client.SetSelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class ProjetoListView extends AbstractListView<ProjetoProxy> implements
		SingleSelectionDataDisplay<ProjetoProxy> {

	private class DescricaoColumn extends Column<ProjetoProxy, String> {
		public DescricaoColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(ProjetoProxy object) {
			return object.getDescricao();
		}
	}

	private class NumeroColumn extends Column<ProjetoProxy, String> {
		public NumeroColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(ProjetoProxy object) {
			return object.getNumero();
		}
	}

	private class TipoInstalacaoColumn extends Column<ProjetoProxy, String> {
		public TipoInstalacaoColumn() {
			super(new TextCell());
		}

		@Override
		public String getValue(ProjetoProxy object) {
			if (object.getTipoInstalacao() != null)
				return object.getTipoInstalacao().getDescricao();
			else
				return null;
		}
	}

	private DataGrid<ProjetoProxy> dataGrid;
	private final SingleSelectionModel<ProjetoProxy> selectionModel;
	private SimplePager pager;
	private Image closeTabImage;
	private GrifoSuggestOracle grifoSuggestOracle;
	private GrifoSearchBox grifoSearchBox;

	public ProjetoListView() {

		VerticalPanel panel = new VerticalPanel();
		panel.setHeight("100%");
		panel.setWidth("100%");
		initWidget(panel);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setWidth("100%");

		panel.add(horizontalPanel);
		dataGrid = GrifoTheme.getGridPadrao(ProjetoProxy.class);
		dataGrid.setPageSize(GrifoSharedConstants.GRID_NUM_ROWS);

		pager = GrifoTheme.getSimplePager();
		pager.setDisplay(dataGrid);
		pager.setSize("250px", "50px");

		grifoSuggestOracle = new GrifoSuggestOracle();
		grifoSearchBox = GrifoTheme.getSearchBox(grifoSuggestOracle);
		grifoSearchBox.setText(GrifoConstants.INSTANCE.pesquisaRapida());
		horizontalPanel.add(grifoSearchBox);
		horizontalPanel.add(pager);

		dataGrid.setSize("100%", "375px");
		panel.add(dataGrid);

		selectionModel = new SingleSelectionModel<ProjetoProxy>();
		dataGrid.setSelectionModel(selectionModel);

		Column<ProjetoProxy, String> nameColumn = new NumeroColumn();
		dataGrid.addColumn(nameColumn, GrifoConstants.INSTANCE.numero());
		Column<ProjetoProxy, String> descriptionColumn = new DescricaoColumn();
		dataGrid.addColumn(descriptionColumn,
				GrifoConstants.INSTANCE.descricao());
		dataGrid.addColumn(new TipoInstalacaoColumn(),
				GrifoConstants.INSTANCE.tipoInstalacao());

		closeTabImage = GrifoTheme.getCloseTabImage();

	}

	@Override
	public SetSelectionModel<ProjetoProxy> getSelectionModel() {
		return selectionModel;
	}

	@Override
	public Widget getTabHeader() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		Label label = GrifoTheme.getLabelPadrao();
		label.setText(GrifoConstants.INSTANCE.projetos());
		horizontalPanel.add(label);
		horizontalPanel.add(closeTabImage);
		return horizontalPanel;
	}

	@Override
	public HasClickHandlers getCloseTabClickHandlers() {
		return closeTabImage;
	}

	@Override
	public HasGrifoSuggestHandler getGrifoSuggestHandler() {
		return grifoSuggestOracle;
	}

	@Override
	public HasSelectionHandlers<Suggestion> getHasSuggestionSelectionHandlers() {
		return grifoSearchBox;
	}

	@Override
	public HasSelectionChangedHandlers getSelectionChangedHandlers() {
		return selectionModel;
	}

	@Override
	public HasData<ProjetoProxy> getData() {
		return dataGrid;
	}

	@Override
	public ProjetoProxy getSelectedItem() {
		return selectionModel.getSelectedObject();
	}

	@Override
	public HasMenuItens getHasMenuItens() {
		return grifoSearchBox;
	}

}
